# Contributor: Michael Truog <mjtruog@protonmail.com>
# Maintainer: Michael Truog <mjtruog@protonmail.com>
#
# INFO
#
# Why is the cloudi.initd file not using the openrc default start-stop-daemon?
#
#   With the Erlang VM it is possible to use the -heart erl command line
#   argument (which is possible with cloudi configuration of the
#   /etc/cloudi/vm.args file).  To allow the use of the heartbeat monitoring
#   of the Erlang runtime system, it is necessary to rely upon the cloudi
#   script for handling the cloudi pid and not utilize openrc for pidfile
#   creation or signal-based termination.
#
#   With a typical Erlang VM release, it is normally expected to provide
#   the ability to attach to the Erlang VM shell of the running node using
#   pipes.  The setup of the Erlang VM shell pipes needs to be handled with
#   Erlang VM execution that also handles detaching the daemon process.
#   If the openrc default start-stop-daemon execution was used instead,
#   it would not be possible to easily setup the Erlang VM shell pipes.
#
#   The termination of cloudi utilizes a 65000 millisecond timeout, allowing
#   all services a maximum of 60000 milliseconds for termination with
#   5000 millisecond for internal Erlang VM delays.  The cloudi script does
#   block for this termination to complete.
#
#   The pid file path is set during the configuration step
#   (shown below with the assignment of CLOUDI_PID_FILE).

pkgname=cloudi
pkgver=2.0.7
pkgrel=7
pkgdesc="Cloud computing framework for efficient, scalable, and stable soft-realtime event processing."
url="https://cloudi.org/"
license="MIT"
# Erlang/OTP and CloudI should both support loongarch64
arch="all"
depends="
	erlang
	g++"
makedepends="autoconf
	automake
	binutils-dev
	boost-dev
	erlang-dev
	erlang-reltool
	gmp-dev
	go
	libseccomp-dev
	libtool
	linux-headers
	nodejs
	perl
	php
	python3-dev
	ruby
	rust
	"
# LXC build containers need limits.cpu config to create CPU quota
# that is detected by Erlang/OTP for creating its schedulers
options="!check"
subpackages="$pkgname-static $pkgname-openrc"
source="https://cloudi.org/download/cloudi-$pkgver.tar.gz
	$pkgname.initd
	"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# configure additional dependencies based on the package architecture

# Enable Haskell on the appropriate architectures
case "$CTARGET_ARCH" in
	x86_64)
		makedepends="$makedepends ghc libffi-dev zlib-dev"
		_configure_args="$_configure_args --enable-haskell-support"
		;;
esac
# Disable Java on architectures that lack OpenJDK
case "$CTARGET_ARCH" in
	x86|armhf|armv7|x86_64|aarch64|ppc64le|s390x)
		# The oldest version of OpenJDK available as an Alpine Linux
		# port is used to avoid blocking the use of the
		# Java CloudI API with older Java dependencies
		# (the Java CloudI API maven package can be used separately,
		#  if necessary, https://github.com/CloudI/cloudi_api_java).
		makedepends="$makedepends openjdk8"
		export PATH="/usr/lib/jvm/java-1.8-openjdk/bin:$PATH"
		;;
	*)
		_configure_args="$_configure_args --disable-java-support"
		;;
esac
# Enable OCaml on the appropriate architectures
case "$CTARGET_ARCH" in
	riscv*)
		;;
	loongarch*)
		;;
	*)
		makedepends="$makedepends ocaml ocaml-compiler-libs"
		_configure_args="$_configure_args --enable-ocaml-support"
		;;
esac

prepare() {
	default_prepare
	cd "$builddir"/src
	./autogen.sh
}

build() {
	cd "$builddir"/src
	PHP="/usr/bin/php8" CLOUDI_PID_FILE="/run/cloudi.pid" ./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--bindir=/usr/sbin \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--host="$CHOST" \
		--build="$CBUILD" \
		--with-test-timeout=container \
		--enable-go-support \
		--enable-rust-support $_configure_args
	make
}

check() {
	cd "$builddir"/src
	make ct
}

package() {
	cd "$builddir"/src
	make DESTDIR="$pkgdir" install
	install -m755 -D "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
1ab46b698d19ea96ae151dfbf332b11ea523178eea039fcab85b4316cc1934fc0867550d0028cbb177839ac82a45bd96b98905cfbe69e1e3913dca7cca01d8e7  cloudi-2.0.7.tar.gz
053f860f656617012208a0e1909e43b2f8979d381a90d3fa5a745a9cf5021fe54556a641fef2badaa9955f2394d00e32183389cb9b501169dcbf8cbe4fbb0329  cloudi.initd
"
